package cn.m3a1.creditsystem.service;

import cn.m3a1.creditsystem.bean.SchoolSchedule;
import cn.m3a1.creditsystem.bean.SchoolScheduleExample;
import cn.m3a1.creditsystem.bean.SchoolScheduleModel;
import cn.m3a1.creditsystem.dao.SchoolScheduleMapper;
import cn.m3a1.creditsystem.dao.SchoolScheduleModelMapper;
import cn.m3a1.creditsystem.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wyj
 * @date 2020/1/11
 * @time 下午7:49
 */
@Service
@Transactional
public class SchoolScheduleService  {
    @Autowired
    SchoolScheduleMapper scheduleMapper;
    @Autowired
    SchoolScheduleModelMapper scheduleModelMapper;
//    id    这门课的id
//    course_code	课程代码
//    course_name	课程名称
//    teacher_id    教师id
//    teacher_name   教师姓名
//    job_title   职称
//    week_time 每周课时
//    start_week 起始周
//    last_week   结束周
//    credit	学分
//    time		上课时间
//    course_nature  课程性质
//    course_category 课程类别
//    class_information   合班信息
//    college  开放学院
    public Boolean inserts(List<List<String>> list) {

        for (int i = 1; i < list.size(); i++) {
            List<String> lo = list.get(i);
            //TODO 随意发挥
            SchoolSchedule schedule=new SchoolSchedule();
            schedule.setCourseCode((String)lo.get(0));
            schedule.setCourseName((String)lo.get(1));
            schedule.setTeacherId((String)lo.get(2));
            schedule.setTeacherName((String)lo.get(3));
            schedule.setJobTitle((String)lo.get(4));
            schedule.setWeekTime((String)lo.get(6));
            if (!lo.get(7).isEmpty()){
                String[] strs = lo.get(7).split("-");
                schedule.setStartWeek(Integer.parseInt(strs[0]));
                schedule.setLastWeek(Integer.parseInt(strs[1]));
            }

            schedule.setCredit(Double.parseDouble(lo.get(8)));
            schedule.setTime((String)lo.get(9));
            schedule.setCourseNature((String)lo.get(19));
            schedule.setCourseCategory((String)lo.get(20));
            schedule.setClassInformation((String)lo.get(23));
            schedule.setCollege((String)lo.get(25));
            int insert = scheduleMapper.insert(schedule);
//            SchoolSchedule schedule=new SchoolSchedule((int)lo.get(0),lo.get(0),lo.get(0),lo.get(0),lo.get(0),lo.get(0),lo.get(0),lo.get(0),
//                    lo.get(0),lo.get(0),lo.get(0),lo.get(0),lo.get(0),lo.get(0),lo.get(0));
            // System.out.println(lo);


        }
        return true;
    }
    public  List<SchoolSchedule> selectAll(){
        SchoolScheduleExample scheduleExample =new SchoolScheduleExample();
        List<SchoolSchedule> schoolSchedules = scheduleMapper.selectByExample(scheduleExample);
        return schoolSchedules;

    }
    public  List<SchoolSchedule> selectSchool(SchoolSchedule schedule){
        SchoolScheduleExample scheduleExample =new SchoolScheduleExample();
        SchoolScheduleExample.Criteria criteria = scheduleExample.createCriteria();
        if (schedule.getCourseCode() !=null){
            criteria.andCourseCodeLike("%"+schedule.getCourseCode().trim()+"%");
        }
        if (schedule.getCourseName() !=null){
            criteria.andCourseNameLike("%"+schedule.getCourseName().trim()+"%");
        }
        if (schedule.getTeacherId() !=null){
            criteria.andTeacherIdLike("%"+schedule.getTeacherId().trim()+"%");
        }
        if (schedule.getTeacherName() !=null){
            criteria.andTeacherNameLike("%"+schedule.getTeacherName().trim()+"%");
        }
        if (schedule.getCollege() !=null){
            criteria.andCollegeLike("%"+schedule.getCollege().trim()+"%");
        }

        List<SchoolSchedule> schoolSchedules = scheduleMapper.selectByExample(scheduleExample);
        return schoolSchedules;

    }
    public Boolean  inserts_model(List<SchoolSchedule> list){
        for (SchoolSchedule sche:list
                ) {
            //SchoolScheduleModel schoolScheduleModel=new SchoolScheduleModel();
            MyUtil myUtil =new MyUtil();
            if (myUtil.getSchoolScheduleModel(sche)==null){
                continue;
            }
            List<SchoolScheduleModel> schoolScheduleModels = myUtil.getSchoolScheduleModel(sche);

            for (SchoolScheduleModel schoolScheduleModel:
            schoolScheduleModels) {
                scheduleModelMapper.insert(schoolScheduleModel);
            }

            //System.out.println(sche.getId());
        }
        return true;
    }


}
